package com.example.demo111111.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.example.demo111111.dao.AuditMerchantDao;
import com.example.demo111111.dao.AuditProductDao;
import com.example.demo111111.dao.MerchantDao;
import com.example.demo111111.dao.ProductDao;
import com.example.demo111111.entity.AuditMerchant;
import com.example.demo111111.entity.Merchant;
import com.example.demo111111.Result.PageResult;
import com.example.demo111111.service.MerchantService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class MerchantServiceImpl implements MerchantService {


    @Resource
    private MerchantDao merchantDao;
    @Resource
    private ProductDao productDao;

    @Resource
    private AuditMerchantDao auditMerchantDao;
    @Override
    @Transactional
    public Boolean save(Merchant merchant) {
        if (merchant.getStatus()==null){
            merchant.setStatus(1);
        }
        merchant.setCreate_time(LocalDateTime.now());
        merchant.setUpdate_time(LocalDateTime.now());

         Boolean result=merchantDao.save(merchant);
         if (result){
             AuditMerchant auditMerchant=new AuditMerchant();
             BeanUtil.copyProperties(merchant,auditMerchant);
             auditMerchant.setMerchant_id(merchant.getId());
             auditMerchantDao.save(auditMerchant);
         }

        return result;
    }

    @Override
    public Boolean update(Merchant merchant) {
        merchant.setUpdate_time(LocalDateTime.now());
        Boolean result=merchantDao.update(merchant);
        return result;
    }


    public PageResult pageQuery(Merchant merchant) {
        PageHelper.startPage(merchant.getPage(),merchant.getPageSize());
        Page<Merchant> page=merchantDao.pageQuery(merchant);
        long total=page.getTotal();
        List<Merchant> records=page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public Boolean deltete(List<Long> ids) {

        Boolean result = null;
        for (Long id : ids) {
            productDao.deleteByMerchantId(id);
            auditMerchantDao.deleteByMerchantId(id);
            result = merchantDao.deleteById(id);
        }
        return result;
    }
}
